package com.bob.web.common.advice;

import com.bob.web.common.constant.CodeConstant;
import com.bob.web.common.constant.ResponseResult;
import com.bob.web.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * 名称: GlobalExceptionHandler<br>
 * 描述:统一异常处理<br>
 * 类型: JAVA<br>
 * 最近修改时间:2017/7/7 15:04<br>
 *
 * @author BoYangsh
 * @version [版本号, V1.0]
 * @since 2017/7/7 15:04
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

	//自定义异常处理
	@ExceptionHandler(ServiceException.class)
	@ResponseBody //这个注解是为了json
	public ResponseResult serviceExceptionHandler(ServiceException e, HttpServletResponse response) {
		ResponseResult responseResult = new ResponseResult(e.getCode());
		return responseResult;
	}

	//自定义异常之后的异常处理
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public ResponseResult serviceExceptionHandler(Exception e, HttpServletResponse response) {
		log.error("=============系统异常:", e);
		ResponseResult responseResult = new ResponseResult(CodeConstant.SYSTEM_ERROR);
		return responseResult;
	}
}

